import sys
from bs4 import BeautifulSoup
import urllib.request as urllib2
import urllib
import re
import os
import saveToDB as DB

def save(url, in_dir, item_name,page_from,catalog_photo_id):

	test_url = url
	response2 = urllib2.urlopen(test_url)
	html2 = response2.read()

	soup2 = BeautifulSoup(html2)

	img_classes2 = soup2.findAll('ul', {'id': 'thumblist'})

	print("URl=" + url)
	i=0
	for im in img_classes2:

		elems = im.findAll('a')

		for eli in elems:
			src = eli.get('href')

			print("sav_sm_mid src = " + src)
			rel = eli.get('rel')
			imgsm = eli.find('img').get('src')

			filenamesm = imgsm.replace('ProdImages','')
			filenamesm = filenamesm.replace('prodimages','')
			filenamesm = filenamesm.replace('/','')

			file_name_large = src.replace('ProdImages','')
			file_name_large = file_name_large.replace('prodimages','')
			file_name_large = file_name_large.replace('/','')
			print("file_name_large src = " + file_name_large)


			relegx = '(rel=")(.|\n)+((">)|(" >)){1}'
			smalimageex = '(smallImage: )(.|\n)+(?!>)'		

			relegxstr  = re.search('(rel=")(.|\n)+((">)|(" >)){1}', str(eli)).group(0)

			smallImagestr = re.search('(smallImage: )(.|\n)+(?!>)', relegxstr).group(0)
			smallImagestr = smallImagestr.replace('smallImage: ','')
			smallImagestr = smallImagestr.replace(' ">','')
			
			smallImagestr = smallImagestr.replace('\'','')

			imgmid = smallImagestr
			smallImagestr = smallImagestr.replace('ProdImages','')
			smallImagestr = smallImagestr.replace('prodimages','')
			smallImagestr = smallImagestr.replace('/','')
			filenamemid = smallImagestr

			newpath = in_dir
			if not os.path.exists(newpath): os.makedirs(newpath)

			url_path = 'http://www.doorstore.co.uk'
			full_urlsm = url_path + imgsm
			full_urlsm = full_urlsm.replace(' ','%20')

			full_url_large = url_path + src
			full_url_large = full_url_large.replace(' ','%20')
						
			image_on_web = urllib2.urlopen(full_urlsm)
			buf = image_on_web.read()

			downloaded_image = open(newpath + '\\'+filenamesm, "wb")
			downloaded_image.write(buf)
			downloaded_image.close()
			image_on_web.close()

			#savePhotoToBd(filenamesm)

			photo_name = filenamesm
			folder = in_dir

			
			photo_type = 'small_image'
			item_name  = item_name
			small_img_id = DB.savePhotoToBd(photo_name, folder, photo_type, item_name,page_from)

			

			#---------------------------------------------------------------------------

			image_on_web = urllib2.urlopen(full_url_large)
			buf = image_on_web.read()

			downloaded_image = open(newpath + '\\'+file_name_large, "wb")
			downloaded_image.write(buf)
			downloaded_image.close()
			image_on_web.close()

			#savePhotoToBd(filenamesm)

			photo_name = file_name_large
			folder = in_dir
			photo_type = 'large_image'
			item_name  = item_name
			large_img_id = DB.savePhotoToBd(photo_name, folder, photo_type, item_name,page_from)
			print("catalog_photo_id === " + str(catalog_photo_id))
			if i==0 :
				DB.setCatalogLargePhoto(catalog_photo_id,large_img_id)
			i= i+1

			

			#---------------------------------------------------------------------------


			full_urlmid = url_path + imgmid
			full_urlmid = full_urlmid.replace(' ','%20')
			

			#print('full_urlmid = ',full_urlmid)
			
			try:
				image_on_web = urllib2.urlopen(full_urlmid)
			except :
				print("error")
				return 
			#nb = input('Choose a number')
			buf = image_on_web.read()

			downloaded_image = open(newpath + '\\'+filenamemid, "wb")
			downloaded_image.write(buf)
			downloaded_image.close()
			image_on_web.close()


			photo_name = filenamemid
			folder = in_dir
			photo_type = 'midle_image'
			item_name  = item_name
			middle_img_id = DB.savePhotoToBd(photo_name, folder, photo_type, item_name,page_from)


			print("small_img_id="+ str(small_img_id))
			print("large_img_id="+ str(large_img_id))
			print("small_img_id="+ str(middle_img_id))
			DB.setLargePhoto(small_img_id,large_img_id)
			DB.setLargePhoto(middle_img_id,large_img_id)


			DB.setMiddlePhoto(small_img_id,middle_img_id)
			DB.setMiddlePhoto(large_img_id,middle_img_id)

			DB.setSmallPhoto(middle_img_id,small_img_id)
			DB.setSmallPhoto(large_img_id,small_img_id)





			#---------------------------------------------------------------------------

			#print(src)
			#print(rel)
			#print(img)
			#print('!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!')

	"""test_url_backspace = 'http://93.72.16.48/door-market/productimg/door 1 lar.jpg'

	print(test_url_backspace)

	test_url_without_backspace = test_url_backspace.replace(' ','%20')

	print(test_url_without_backspace)

	test_url =  'http://www.doorstore.co.uk/ProdImages/Contemporary%204%20Lite.jpg'
	image_on_web = urllib2.urlopen(test_url)

	print(image_on_web)
	buf = image_on_web.read()

	downloaded_image = open('test34.jpg', "wb")
	downloaded_image.write(buf)
	downloaded_image.close()
	image_on_web.close()"""

	#print('*************************************************************')

	"""
	i=0
	for im in img_classes:
		src = im.find('a').get('href')
		print('---------------------------------------------------------------------')
		print(src)

		file_name = src.replace('/ProdImages/','')
		file_name = file_name.replace('/prodimages/','')
		
		
		
		url_path = 'http://www.doorstore.co.uk'
		full_url = url_path + src
		full_url = full_url.replace(' ','%20')
		print(full_url)
		print(file_name)
		#if i == 0:
		image_on_web = urllib2.urlopen(full_url)
		buf = image_on_web.read()

		downloaded_image = open(file_name, "wb")
		downloaded_image.write(buf)
		downloaded_image.close()
		image_on_web.close()

		print('---------------------------------------------------------------------')"""

	#print('12')